﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace CCSAdmin
{
    public static class Commands
    {
        public static readonly RoutedCommand RoutedDeleteUserCommand = new RoutedCommand("Routed Delete User", typeof(Commands));

        public static readonly ICommand DeleteUserCommand = new DeleteUser();
    }

    public class DeleteUser : ICommand
    {
        public static event EventHandler<CommandCompleteEventArgs> CommandCompleted;

        private const string CommandName = "Delete User";

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            CCS.CCS m_ccsWebService = new CCSAdmin.CCS.CCS();
            if (!m_ccsWebService.DeleteUserByEmail(App.Credentials, parameter.ToString()))
            {
                throw new CommandException(string.Format("Unable to delete user with e-mail \"{0}\"", parameter));
            }
            if (CommandCompleted != null)
            {
                CommandCompleted(this, new CommandCompleteEventArgs(CommandName, string.Format("Deleted user with e-mail \"{0}\"", parameter)));
            }
        }
    }

    public class CommandCompleteEventArgs : EventArgs
    {
        public string CommandName { get; private set; }

        public string Message { get; private set; }

        public CommandCompleteEventArgs(string commandName, string message)
        {
            this.CommandName = commandName;
            this.Message = message;
        }
    }

    [Serializable]
    public class CommandException : Exception
    {
        public CommandException()
        {
        }

        public CommandException(string message)
            : base(message)
        {
        }

        public CommandException(string message, Exception inner)
            : base(message, inner)
        {
        }

        protected CommandException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
    }
}
